#!/bin/bash

source ../../pi.vars;
gost_version=`get_github_latest https://github.com/ginuerzh/gost/releases/latest`



project_name='gost'
echo "copy files......"
whoname=`whoami`
sudo mkdir -p /opt/${project_name}/logs;sudo chmod -R 755 /opt/${project_name};
sudo chown -R ${whoname} /opt/${project_name};
if [ -f /opt/${project_name}/gost-linux-armv8 ];then
	sudo systemctl stop ${project_name}||echo 'skip'
fi;
rm -f /opt/${project_name}/*.gz;
wget ${github_proxy}https://github.com/ginuerzh/gost/releases/download/v${gost_version}/gost-linux-armv8-${gost_version}.gz -O /opt/${project_name}/gost-linux-armv8.gz
gunzip -dfc /opt/${project_name}/gost-linux-armv8.gz > /opt/${project_name}/gost-linux-armv8;
cp st*.sh /opt/${project_name}/;
cp *.service /opt/${project_name}/;
chmod -R 755 /opt/${project_name}/;

sed -i "s/User=.*/User=${whoname}/g" /opt/${project_name}/${project_name}.service;
sudo cp /opt/${project_name}/${project_name}.service /lib/systemd/system/${project_name}.service;
sudo systemctl daemon-reload;



if [ -e /opt/${project_name}/gost-tunnel ]; then
    echo "There is having /opt/${project_name}/gost-tunnel , ignore init gost-tunnel!"
	echo "start services......"
	sudo systemctl enable --now ${project_name};
else
    cp gost-tunnel /opt/${project_name}/;
	echo "please config /opt/${project_name}/gost-tunnel, then to start [/opt/${project_name}/start.sh]"

fi;


